package dao.impl;

import dao.OrderDao;
import dao.domain.Book;
import dao.domain.Client;
import dao.domain.Order;
import service.BookService;
import service.UserService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author huangzhiyang
 * @Date 2023/11/7
 * @Description
 */
public class OrderDaoImpl extends BaseDaoImpl implements OrderDao {
    /**
     * 从result中提取数据
     */
    public static List<Order> listOrder(ResultSet resultSet) throws SQLException {
        List<Order> orderList = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String clientAccount = resultSet.getString("client_account");
            int bookNumber = resultSet.getInt("book_number");
            Date orderDate = resultSet.getDate("borrowDate");
            Client client = (Client) UserService.selectByAccount("client", clientAccount);
            Book book = BookService.selectById(bookNumber);
            orderList.add(new Order(id, client, book, orderDate));
        }
        return orderList;
    }

    /**
     * 删除数据库记录
     */
    @Override
    public void delete(Order order) throws SQLException {
        String sql = "delete from `order` where id=?";
        Connection conn = super.getConnection();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setInt(1, order.getId());
        statement.executeUpdate();
        super.closeAll(conn, statement, null);
    }

    /**
     * 储存数据到数据库
     */
    @Override
    public void save(Order order) throws SQLException {
        String sql = "INSERT INTO `order` (book_number,client_account, borrowDate) VALUES (?, ?, NOW())";
        Connection conn = super.getConnection();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(2, order.getClient().getAccount());
        statement.setInt(1, order.getBook().getNumber());
        statement.executeUpdate();
        super.closeAll(conn, statement, null);
    }

    /**
     * 根据订单号搜索数据库
     *
     * @param id 订单号
     */
    @Override
    public Order selectById(int id) throws SQLException {
        Order order = new Order();
        Connection connection = super.getConnection();
        PreparedStatement statement = connection.prepareStatement(
                "SELECT * FROM `order` WHERE id=" + id + ";"
        );
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            String clientAccount = resultSet.getString("client_account");
            int bookNumber = resultSet.getInt("book_number");
            Date orderDate = resultSet.getDate("borrowDate");
            Client client = (Client) UserService.selectByAccount("client", clientAccount);
            Book book = BookService.selectById(bookNumber);
            order.setId(id);
            order.setBook(book);
            order.setClient(client);
            order.setOrderDate(orderDate);
            System.out.println(order);
        }
        return order;
    }

    /**
     * 根据用户搜索数据库
     *
     * @param client 借书的客户
     */
    @Override
    public List<Order> selectByClient(Client client) throws SQLException {
        List<Order> orders;
        Connection connection = super.getConnection();
        PreparedStatement statement = connection.prepareStatement(
                "SELECT * FROM `order` WHERE client_account='" + client.getAccount() + "'"
        );

        ResultSet resultSet = statement.executeQuery();
        orders = listOrder(resultSet);
        super.closeAll(connection, statement, resultSet);
        return orders;
    }

    /**
     * 列出所有借阅订单记录
     */
    @Override
    public List<Order> selectAll() throws SQLException {
        List<Order> orders;
        try (Connection connection = super.getConnection();
             PreparedStatement statement = connection.prepareStatement(
                     "SELECT * FROM `order`")
        ) {
            ResultSet resultSet = statement.executeQuery();
            orders = listOrder(resultSet);
        }
        return orders;
    }
}
